<?php
@ob_start();
@session_start();

include("config/connect.php");
include("function/thaidate.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>-</title>
<link rel="stylesheet" href="stylesheet/stylereport.css" type="text/css" />
<link rel="shortcut icon" href="image/shopping-bag.ico" />
<style type="text/css" media="print">
input{
	display:none;
}
</style>
</head>
<body>
<h1>สรุปรายจ่าย เฮือนคำ รีสอร์ท</h1>
<?php
if($_POST['rtype']=='day' && $_POST['dateInput']!=""){
	$dmy = $_POST["dateInput"];
	$day = substr($dmy, 0, 2);
	$month = substr($dmy, 3, 2);
	$y = substr($dmy, 6, 4);
	$year = $y-543;
	//$date = ($y - 543) . '-' . $m . '-' . $d;
	//$date = date("Y-m-d", $adate);
	
	$date = $year."-".$month."-".$day;
	
	echo "<h2>ประจำ". thaidate(strtotime($date)) ."</h2>";
	
	//$Query = mysql_query("SELECT product.category_id as category_id,sum(buy_detail.total) as total, category.category_name FROM buy_history,buy_detail,product,category WHERE category.category_id = product.category_id AND buy_history.buy_his_id = buy_detail.buy_his_id AND product.product_id = buy_detail.product_id AND buy_history.buy_his_date = '$date' GROUP BY product.category_id");
	$Query = mysql_query("SELECT buy_his_date FROM buy_history WHERE buy_his_date = '$date' GROUP BY buy_his_date");
	
} else if($_POST['rtype']=='now' && $_POST['DateNow']!="") {

	$date = $_POST['DateNow'];
	
	echo "<h2>ประจำ". thaidate(strtotime($date)) ."</h2>";
	
	//$Query = mysql_query("SELECT product.category_id as category_id,sum(buy_detail.total) as total, category.category_name FROM buy_history,buy_detail,product,category WHERE category.category_id = product.category_id AND buy_history.buy_his_id = buy_detail.buy_his_id AND product.product_id = buy_detail.product_id AND buy_history.buy_his_date = '$date' GROUP BY product.category_id");
	$Query = mysql_query("SELECT buy_his_date FROM buy_history WHERE buy_his_date = '$date' GROUP BY buy_his_date");
	
} else if($_POST['rtype']=='month' && $_POST['monthSelect']!="" && $_POST['yearSelect']!=""){
	
	$monthname = array("มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฎาคม","สิงหาคม","กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม");
	$year = $_POST['yearSelect']+543;
	echo "<h2>ประจำเดือน ".$monthname[$_POST['monthSelect']-1]." พ.ศ. ". $year ."</h2>";
	
	$Query = mysql_query("SELECT buy_his_date FROM buy_history WHERE month(buy_his_date) = '".$_POST['monthSelect']."' AND year(buy_his_date) = '".$_POST['yearSelect']."' GROUP BY buy_his_date");
	//$Query = mysql_query("SELECT product.category_id as category_id,sum(buy_detail.total) as total, category.category_name FROM buy_history,buy_detail,product,category WHERE category.category_id = product.category_id AND buy_history.buy_his_id = buy_detail.buy_his_id AND product.product_id = buy_detail.product_id AND month(buy_history.buy_his_date) = '".$_POST['monthSelect']."' AND year(buy_history.buy_his_date) = '".$_POST['yearSelect']."' GROUP BY product.category_id");	
	
} else if($_POST['rtype']=='dateto' && $_POST['dateInput1']!="" && $_POST['dateInput2']!=""){
	
	$dmy = $_POST["dateInput1"];
	$day = substr($dmy, 0, 2);
	$month = substr($dmy, 3, 2);
	$y = substr($dmy, 6, 4);
	$year = $y-543;
	$date1 = $year."-".$month."-".$day;
	
	$dmy1 = $_POST["dateInput2"];
	$day1 = substr($dmy1, 0, 2);
	$month1 = substr($dmy1, 3, 2);
	$y1 = substr($dmy1, 6, 4);
	$year1 = $y1-543;
	$date2 = $year1."-".$month1."-".$day1;
	
	echo "<h2>ตั้งแต่วันที่ ".thai_date(strtotime($date1))." ถึงวันที่ ".thai_date(strtotime($date2))."</h2>";
	
	//$Query = mysql_query("SELECT product.category_id as category_id,sum(buy_detail.total) as total, category.category_name FROM buy_history,buy_detail,product,category WHERE category.category_id = product.category_id AND buy_history.buy_his_id = buy_detail.buy_his_id AND product.product_id = buy_detail.product_id AND buy_history.buy_his_date BETWEEN '$date1' AND '$date2' GROUP BY product.category_id");
	$Query = mysql_query("SELECT buy_his_date FROM buy_history WHERE buy_his_date BETWEEN '$date1' AND '$date2' GROUP BY buy_his_date");
}
	$numQuery = mysql_num_rows($Query);

	$nQuery = mysql_query("SELECT category_name FROM category ORDER BY category_name");

?>

<table class="listorder2">
<thead>
<tr>
	<td align="center" width="70px">วันที่</td>
<?php 
	while($nRow = mysql_fetch_array($nQuery)){
?>
	<td align="center"><?php echo $nRow['category_name'];?></td>
<?php } ?>
	<td>รวมจ่าย</td>
	<td>หมายเหตุ</td>
</tr>
</thead>
<?php 
//$row = mysql_fetch_array($Query);
while($row = mysql_fetch_array($Query)){
?>
	<tr>
		<td align="right" style="padding-right: 10px;"><?php echo thai_date2(strtotime($row['buy_his_date'])); ?></td>
<?php
$nQuery1 = mysql_query("SELECT category_id FROM category ORDER BY category_name");
while($nRow1 = mysql_fetch_array($nQuery1)){
	$cQuery = mysql_query("SELECT sum(buy_detail.total) as total FROM buy_history,buy_detail,product WHERE buy_history.buy_his_id = buy_detail.buy_his_id AND buy_detail.product_id = product.product_id AND buy_history.buy_his_date = '".$row['buy_his_date']."' AND product.category_id = '".$nRow1['category_id']."' GROUP BY buy_history.buy_his_date,product.category_id");
	$cRow = mysql_fetch_array($cQuery);
	
	$dQuery = mysql_query("SELECT sum(buy_detail.total) as total FROM buy_history,buy_detail WHERE buy_history.buy_his_id = buy_detail.buy_his_id AND buy_history.buy_his_date = '".$row['buy_his_date']."' GROUP BY buy_history.buy_his_date");
	$dRow = mysql_fetch_array($dQuery);
?>
		<td align="right" style="padding-right: 5px;"><?php if($cRow['total']!=""){echo number_format($cRow['total'], 2,'.',',');}?></td>
<?php 

} ?>
	<td align="right" style="padding-right: 5px;"><?php echo number_format($dRow['total'], 2,'.',',');?></td>
	<td>&nbsp;</td>
	</tr>
<?php
	$strtal = $strtal+$dRow['total'];
}
?>
<tfoot>
<tr>
	<td align="center" >รวม</td>
<?php
$nQuery2 = mysql_query("SELECT category_id FROM category ORDER BY category_name");
while($nRow2 = mysql_fetch_array($nQuery2)){
	
if($_POST['rtype']=='day' && $_POST['dateInput']!=""){
	$dmy = $_POST["dateInput"];
	$day = substr($dmy, 0, 2);
	$month = substr($dmy, 3, 2);
	$y = substr($dmy, 6, 4);
	$year = $y-543;

	$date = $year."-".$month."-".$day;
	
	$sQuery = mysql_query("SELECT sum(buy_detail.total) as total FROM buy_history,buy_detail,product WHERE buy_history.buy_his_id = buy_detail.buy_his_id AND buy_detail.product_id = product.product_id AND buy_history.buy_his_date = '$date' AND product.category_id = '".$nRow2['category_id']."' ");
	
} else if($_POST['rtype']=='now' && $_POST['DateNow']!="") {

	$date = $_POST['DateNow'];
	$sQuery = mysql_query("SELECT sum(buy_detail.total) as total FROM buy_history,buy_detail,product WHERE buy_history.buy_his_id = buy_detail.buy_his_id AND buy_detail.product_id = product.product_id AND buy_history.buy_his_date = '$date' AND product.category_id = '".$nRow2['category_id']."' ");
	
} else if($_POST['rtype']=='month' && $_POST['monthSelect']!="" && $_POST['yearSelect']!=""){
	
	$sQuery = mysql_query("SELECT sum(buy_detail.total) as total FROM buy_history,buy_detail,product WHERE buy_history.buy_his_id = buy_detail.buy_his_id AND buy_detail.product_id = product.product_id AND month(buy_history.buy_his_date) = '".$_POST['monthSelect']."' AND year(buy_history.buy_his_date) = '".$_POST['yearSelect']."' AND product.category_id = '".$nRow2['category_id']."' ");

} else if($_POST['rtype']=='dateto' && $_POST['dateInput1']!="" && $_POST['dateInput2']!=""){
	
	$dmy = $_POST["dateInput1"];
	$day = substr($dmy, 0, 2);
	$month = substr($dmy, 3, 2);
	$y = substr($dmy, 6, 4);
	$year = $y-543;
	$date1 = $year."-".$month."-".$day;
	
	$dmy1 = $_POST["dateInput2"];
	$day1 = substr($dmy1, 0, 2);
	$month1 = substr($dmy1, 3, 2);
	$y1 = substr($dmy1, 6, 4);
	$year1 = $y1-543;
	$date2 = $year1."-".$month1."-".$day1;
	
	$sQuery = mysql_query("SELECT sum(buy_detail.total) as total FROM buy_history,buy_detail,product WHERE buy_history.buy_his_id = buy_detail.buy_his_id AND buy_detail.product_id = product.product_id AND buy_history.buy_his_date BETWEEN '$date1' AND '$date2' AND product.category_id = '".$nRow2['category_id']."' ");
}
	//$sQuery = mysql_query("SELECT sum(buy_detail.total) as total FROM buy_history,buy_detail,product WHERE buy_history.buy_his_id = buy_detail.buy_his_id AND buy_detail.product_id = product.product_id AND month(buy_history.buy_his_date) = '".$_POST['monthSelect']."' AND year(buy_history.buy_his_date) = '".$_POST['yearSelect']."' AND product.category_id = '".$nRow2['category_id']."' ");
	
	$sRow = mysql_fetch_array($sQuery);
?>
		<td align="right" style="padding-right: 5px;"><?php echo number_format($sRow['total'], 2,'.',',');?></td>
<?php } ?>
	<td align="right" style="padding-right: 5px;"><?php echo number_format($strtal, 2,'.',',');?></td>
	<td>&nbsp;</td>
</tr>
</tfoot>
</table>


<br /><br />
<div style="padding-top: 20px">
<input type="button" name="endsale" id="endsale" value="พิมพ์รายงาน" onclick="print();" > <input type="button" name="btnClose" value="ปิดหน้าต่าง" onclick="JavaScript:window.close();" /></div>
</body>

</html>
